package com.hddz.acquisition;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import java.lang.ref.WeakReference;

/**
 * Created by 金桔 on 2017/7/26.
 */

public class DialogJst {

    private static ErrorInfoUtil getError(Activity mActivity) {
        SoundUtil mSoundUtil = new SoundUtil(mActivity, true, false);
        ErrorInfoUtil errorUtil = new ErrorInfoUtil(mActivity, mSoundUtil);
        return errorUtil;
    }

    public static void showError(Activity mActivity, Object message) {
        ErrorInfoUtil errorUtil = getError(mActivity);
        errorUtil.showError(message);
    }

    public static void showError(Activity mActivity, Object message, int level) {
        ErrorInfoUtil errorUtil = getError(mActivity);
        errorUtil.showError(message, level);
    }

    public static void showError(Activity mActivity, Object message, int level, EditText edit) {
        ErrorInfoUtil errorUtil = getError(mActivity);
        errorUtil.showError(message, level);
    }

    public static void showError(Activity mActivity, Object message, int level, Handler hander) {
        ErrorInfoUtil errorUtil = getError(mActivity);
        errorUtil.showError(message, level, hander);
    }

    /**
     * 确认对话框
     *
     * @param message
     * @param exHandle
     */
    public static void sendShowMessage(Activity mActivity, String message, final Handler exHandle) {
        if (!mActivity.isFinishing()) {
            try {
                android.app.Dialog dialog = new AlertDialog.Builder(mActivity).setTitle("提示").setMessage(message)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (exHandle != null)
                                    exHandle.sendMessage(new Message());
                            }
                        }).create();
                dialog.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 通用确认对话框
     *
     * @param message
     * @param exHandle
     */
    public static void sendConfrimMessage(Activity mActivity, String message, final Handler exHandle) {
        if (!mActivity.isFinishing()) {
            try {
                android.app.Dialog dialog = new AlertDialog.Builder(mActivity).setTitle("提示").setMessage(message)
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }

                        }).setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (exHandle != null)
                                    exHandle.sendMessage(new Message());
                            }
                        }).create();
                dialog.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 通用确认对话框
     *
     * @param message
     */
    public static void sendConfrimMessage(Activity mActivity, String message, final Handler commitHandle, final Handler cancelHandle) {
        if (!mActivity.isFinishing()) {
            try {
                android.app.Dialog dialog = new AlertDialog.Builder(mActivity).setTitle("提示").setMessage(message)
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                if (cancelHandle != null)
                                    cancelHandle.sendMessage(new Message());
                            }

                        }).setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (commitHandle != null)
                                    commitHandle.sendMessage(new Message());
                            }
                        }).create();
                dialog.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void sendConfrimOpenMessage(Activity mActivity, String message, final Handler yesHandle, final Handler noHandle) {
        if (!mActivity.isFinishing()) {
            try {
                android.app.Dialog dialog = new AlertDialog.Builder(mActivity).setTitle("选择确认").setMessage(message)
                        .setPositiveButton("开启", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (yesHandle != null)
                                    yesHandle.sendMessage(new Message());
                            }
                        }).setNegativeButton("关闭", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (noHandle != null)
                                    noHandle.sendMessage(new Message());
                            }

                        }).create();
                dialog.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 通用确认对话框
     *
     * @param message
     */
    public static void sendConfrimOpenMessageWithAction(Activity mActivity, String message, DialogInterface.OnClickListener postive, DialogInterface.OnClickListener negative) {
        if (!mActivity.isFinishing()) {
            try {
                android.app.Dialog dialog = new AlertDialog.Builder(mActivity).setTitle("提示").setMessage(message)
                        .setNegativeButton("取消", negative).setPositiveButton("确认", postive).create();
                dialog.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void sendConfrimOpenMessage(Activity mActivity, String message, final Handler yesHandle, final Handler noHandle, String title1, String title2) {
        if (!mActivity.isFinishing()) {
            try {
                android.app.Dialog dialog = new AlertDialog.Builder(mActivity).setTitle("选择确认").setMessage(message)
                        .setPositiveButton(title1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (yesHandle != null)
                                    yesHandle.sendMessage(new Message());
                            }
                        }).setNegativeButton(title2, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (noHandle != null)
                                    noHandle.sendMessage(new Message());
                            }

                        }).create();
                dialog.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    private static Dialog prosDialog;

    private static Dialog createLoadingDialog(final Context context) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setContentView(v, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progress);
        Drawable drawable = context.getResources().getDrawable(R.drawable.loading_animation_blue);
        progressBar.setIndeterminateDrawable(drawable);
        return loadingDialog;
    }

    public Dialog createLoadingDialogNew(Context context) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progress);
        Drawable drawable = context.getResources().getDrawable(R.drawable.loading_animation_blue);
        progressBar.setIndeterminateDrawable(drawable);
        return loadingDialog;
    }

    /**
     * 开始loading动画
     */
    public static void startLoading(Activity mActivity) {
        try {
            if (prosDialog != null) {
                prosDialog.dismiss();
                prosDialog = null;
            }
            WeakReference<Activity> weakReference = new WeakReference(mActivity);
            prosDialog = createLoadingDialog(weakReference.get());
            prosDialog.setCanceledOnTouchOutside(false);
            prosDialog.setCancelable(true);// 设置进度条是否可以按退回键取消
            prosDialog.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止loading动画
     */
    public static void stopLoading() {
        try {
            if (prosDialog != null && prosDialog.isShowing()) {
//                mHandler.removeCallbacks(runLoading);
                prosDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static boolean isLoading() {
        try {
            if (prosDialog != null && prosDialog.isShowing()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
